package com.snowfield.lb47.lbconfig.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.snowfield.lbcommon.config.SnowFieldConfig;
import com.snowfield.lb47.lbconfig.domain.BlackwhiteList;
import com.snowfield.lb47.lbconfig.domain.BlackwhiteListDetail;
import com.snowfield.lb47.lbconfig.mapper.BlackwhiteListDetailMapper;
import com.snowfield.lb47.lbconfig.mapper.BlackwhiteListMapper;
import com.snowfield.lb47.lbconfig.service.IBlackwhiteListDetailService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 黑白名单详情Service业务层处理
 *
 * @author snowfield
 * @date 2024-05-24
 */
@Service
public class BlackwhiteListDetailServiceImpl implements IBlackwhiteListDetailService {
    @Autowired
    private BlackwhiteListDetailMapper blackwhiteListDetailMapper;
    @Autowired
    private BlackwhiteListMapper blackwhiteListMapper;

    /**
     * 查询黑白名单详情
     *
     * @param id 黑白名单详情主键
     * @return 黑白名单详情
     */
    @Override
    public BlackwhiteListDetail selectBlackwhiteListDetailById(Long id) {
        return blackwhiteListDetailMapper.selectBlackwhiteListDetailById(id);
    }

    /**
     * 查询黑白名单详情列表
     *
     * @param blackwhiteListDetail 黑白名单详情
     * @return 黑白名单详情
     */
    @Override
    public List<BlackwhiteListDetail> selectBlackwhiteListDetailList(BlackwhiteListDetail blackwhiteListDetail) {
        return blackwhiteListDetailMapper.selectBlackwhiteListDetailList(blackwhiteListDetail);
    }

    /**
     * 新增黑白名单详情
     *
     * @param blackwhiteListDetail 黑白名单详情
     * @return 结果
     */
    @Override
    public int insertBlackwhiteListDetail(BlackwhiteListDetail blackwhiteListDetail) {
        blackwhiteListDetail.setCreateTime(DateUtils.getNowDate());
        Integer affectRows = blackwhiteListDetailMapper.insertBlackwhiteListDetail(blackwhiteListDetail);

        if (affectRows > 0) {
            BlackwhiteList blackwhiteList = blackwhiteListMapper.selectBlackwhiteListById(blackwhiteListDetail.getListId());
            createBlackwhiteListFile(blackwhiteListDetail.getListId(), blackwhiteList.getListType());
        }

        return affectRows;
    }

    /**
     * 修改黑白名单详情
     *
     * @param blackwhiteListDetail 黑白名单详情
     * @return 结果
     */
    @Override
    public int updateBlackwhiteListDetail(BlackwhiteListDetail blackwhiteListDetail) {
        blackwhiteListDetail.setUpdateTime(DateUtils.getNowDate());
        Integer affectRows = blackwhiteListDetailMapper.updateBlackwhiteListDetail(blackwhiteListDetail);

        if (affectRows > 0) {
            BlackwhiteList blackwhiteList = blackwhiteListMapper.selectBlackwhiteListById(blackwhiteListDetail.getListId());
            createBlackwhiteListFile(blackwhiteListDetail.getListId(), blackwhiteList.getListType());
        }

        return affectRows;
    }

    /**
     * 批量删除黑白名单详情
     *
     * @param ids 需要删除的黑白名单详情主键
     * @return 结果
     */
    @Override
    public int deleteBlackwhiteListDetailByIds(Long listId, Long[] ids) {
        Integer affectRows = blackwhiteListDetailMapper.deleteBlackwhiteListDetailByIds(ids);
        if (affectRows > 0) {
            BlackwhiteList blackwhiteList = blackwhiteListMapper.selectBlackwhiteListById(listId);
            createBlackwhiteListFile(listId, blackwhiteList.getListType());
        }
        return affectRows;
    }

    /**
     * 删除黑白名单详情信息
     *
     * @param id 黑白名单详情主键
     * @return 结果
     */
    @Override
    public int deleteBlackwhiteListDetailById(Long id) {
        return blackwhiteListDetailMapper.deleteBlackwhiteListDetailById(id);
    }

    /**
     * 校验黑白名单中的IP地址是否唯一
     *
     * @param blackwhiteListDetail 黑白名单IP详情
     * @return 结果
     */
    @Override
    public Boolean checkIpUnique(BlackwhiteListDetail blackwhiteListDetail) {
        Long listId = blackwhiteListDetail.getListId();
        BlackwhiteListDetail detail = blackwhiteListDetailMapper.selectBlackwhiteListDetailByIpaddr(listId, blackwhiteListDetail.getIpaddr());
        if (detail != null && !detail.getId().equals(blackwhiteListDetail.getId())) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * 生成黑白名单列表文件
     *
     * @param listId   名单标识ID
     * @param listType 名单类型，black or white
     */
    private void createBlackwhiteListFile(Long listId, String listType) {
        //文件名称默认为：{listType}_id.lst
        String listFileName = String.format("%s-%d.lst", listType, listId);
        String listFilePath = SnowFieldConfig.getBasicConfigPath() + "/blackwhiteList/" + listFileName;

        BlackwhiteListDetail queryForm = new BlackwhiteListDetail();
        queryForm.setListId(listId);
        List<BlackwhiteListDetail> blackwhiteListDetailList = selectBlackwhiteListDetailList(queryForm);

        List<String> blackwhiteLstFileContent = new ArrayList<>();
        for (BlackwhiteListDetail detail : blackwhiteListDetailList) {
            if (detail.getStatus().equals("0")){
                continue;
            }
            if (detail.getIpType().equals("ip")) {
                blackwhiteLstFileContent.add(detail.getIpaddr());
            } else if (detail.getIpType().equals("subnet")) {
                blackwhiteLstFileContent.add(detail.getIpaddr() + "/" + detail.getMask());
            }
        }

        try {
            FileUtils.writeLines(new File(listFilePath), blackwhiteLstFileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
